--    Program: 		CAPAREXT_RECEIPT_DIST.sql
--    Description:  Extract AR Receipt Distribution information 
--    Revision:     1st     
--    Parameter: 	1. set of books short name e.g. SOB_C999

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;
alter session set nls_date_format='YYYYMMDD HH24:MI:SS';

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
                    		
   l_utl_file       		utl_file.file_type;
   l_filename       		varchar2(255);
   l_pathname       		varchar2(100);
                    		
   l_ngo_sob				varchar2(20);
   
   l_set_of_books_id		number;
                    		  
   cursor cur_receipt_dist (p_set_of_books_id number) is
      select ad.* 
      from   ar_distributions_all ad,
             hr_organization_information hoi
	  where  hoi.org_information_context = 'Operating Unit Information'
	  and    hoi.org_information3 = to_char(p_set_of_books_id)
	  and    hoi.organization_id = ad.org_id;

begin
   l_pathname := '/tmp';
   l_filename := 'CAPAR_RECEIPT_DIST.dat';
   l_utl_file := utl_file.fopen(l_pathname, l_filename, 'w');
   
   l_processed_count := 0;
   l_rejected_count  := 0;
   
   l_ngo_sob := '&1';
   
   begin
     select set_of_books_id
     into   l_set_of_books_id
     from   gl_sets_of_books
     where  upper(short_name) = upper(l_ngo_sob);
   exception
     when others then
       dbms_output.put_line(l_ngo_sob||': set of books name not found');
       --raise;
   end;      

   for cur in cur_receipt_dist(l_set_of_books_id) loop   	
     
     -- output data 
     utl_file.put_line( l_utl_file, 
       cur.LINE_ID                   || chr(9) ||
       cur.SOURCE_ID                 || chr(9) ||
       cur.SOURCE_TABLE              || chr(9) ||
       cur.SOURCE_TYPE               || chr(9) ||
       cur.CODE_COMBINATION_ID       || chr(9) ||
       cur.AMOUNT_DR                 || chr(9) ||
       cur.AMOUNT_CR                 || chr(9) ||
       cur.ACCTD_AMOUNT_DR           || chr(9) ||
       cur.ACCTD_AMOUNT_CR           || chr(9) ||
       cur.CREATION_DATE             || chr(9) ||
       cur.CREATED_BY                || chr(9) ||
       cur.LAST_UPDATED_BY           || chr(9) ||
       cur.LAST_UPDATE_DATE          || chr(9) ||
       cur.LAST_UPDATE_LOGIN         || chr(9) ||
       cur.ORG_ID                    || chr(9) ||
       cur.SOURCE_TABLE_SECONDARY    || chr(9) ||
       cur.SOURCE_ID_SECONDARY       || chr(9) ||
       cur.CURRENCY_CODE             || chr(9) ||
       cur.CURRENCY_CONVERSION_RATE  || chr(9) ||
       cur.CURRENCY_CONVERSION_TYPE  || chr(9) ||
       cur.CURRENCY_CONVERSION_DATE  || chr(9) ||
       cur.TAXABLE_ENTERED_DR        || chr(9) ||
       cur.TAXABLE_ENTERED_CR        || chr(9) ||
       cur.TAXABLE_ACCOUNTED_DR      || chr(9) ||
       cur.TAXABLE_ACCOUNTED_CR      || chr(9) ||
       cur.TAX_LINK_ID               || chr(9) ||
       cur.THIRD_PARTY_ID            || chr(9) ||
       cur.THIRD_PARTY_SUB_ID        || chr(9) ||
       cur.REVERSED_SOURCE_ID        || chr(9) ||
       cur.TAX_CODE_ID               || chr(9) ||
       cur.LOCATION_SEGMENT_ID       || chr(9) ||
       cur.SOURCE_TYPE_SECONDARY     || chr(9) ||
       cur.TAX_GROUP_CODE_ID
	 );                        
                               
	 l_processed_count := l_processed_count + 1;
   end loop;

   if l_processed_count = 0 then   
     l_rejected_count := l_rejected_count + 1;
     l_err_msg := l_ngo_sob || ': AR Receipt Distribution not found';
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPAREXT_RECEIPT_DIST', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;

   utl_file.fclose(l_utl_file);
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/

--exit
